package com.interauto.Controller.casepage;


import com.interauto.Dao.entity.Interinfo;
import com.interauto.bean.web.InterinfoWeb;
import com.interauto.bean.web.ResPonseinfo;
import com.interauto.service.web.CasePageService;
import com.interauto.service.web.InterinfoPageService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * 操作接口相关的api
 */
@Controller
@RequestMapping(value = "/api/interinfo")
public class InterinfoApiController {
    private static final Logger logger= LoggerFactory.getLogger(InterinfoApiController.class);
    @Autowired
    private CasePageService casePageService;
    @Autowired
    private InterinfoPageService interinfoPageService;


    @RequestMapping(value = "/addinterinfo",method = RequestMethod.POST)
    @ResponseBody
    public Object addinterinfo(@RequestBody InterinfoWeb interinfoWeb){
        if(interinfoWeb == null){
            return new ResPonseinfo(-1,"请求对象为空");
        }
        logger.info("addinterinfo info is --->>>"+interinfoWeb.toString());
        if(interinfoWeb.getCaseid() == null || interinfoWeb.getCaseid()<1){
            return new ResPonseinfo(-2,"保存的用例ID参数为空");
        }
        if(StringUtils.isEmpty(interinfoWeb.getInterfacename()) || StringUtils.isEmpty(interinfoWeb.getHttpurl())){
            return new ResPonseinfo(-3,"保存的参数有空");
        }
        boolean addstatus=interinfoPageService.addinterinfo(interinfoWeb);
        if(addstatus){
            return new ResPonseinfo(0,"保存成功");
        }
        return new ResPonseinfo(500,"保存失败,系统异常");
    }


    @RequestMapping(value = "/upinterinfo",method = RequestMethod.POST)
    @ResponseBody
    public Object upinterinfo(@RequestBody InterinfoWeb interinfoWeb){
        if(interinfoWeb == null){
            return new ResPonseinfo(-1,"请求对象为空");
        }
        logger.info("upinterinfo info is --->>>"+interinfoWeb.toString());
        if(interinfoWeb.getCaseid() == null || interinfoWeb.getCaseid()<1){
            return new ResPonseinfo(-2,"保存的用例ID参数为空");
        }
        if(StringUtils.isEmpty(interinfoWeb.getInterfacename()) || StringUtils.isEmpty(interinfoWeb.getHttpurl())){
            return new ResPonseinfo(-3,"保存的参数有空");
        }
        interinfoPageService.upinterinfo(interinfoWeb);
        return new ResPonseinfo(0,"保存成功");

    }

    @RequestMapping(value = "/getinterinfo",method = RequestMethod.POST)
    @ResponseBody
    public Object getInterinfo(@RequestParam(value = "interid") Integer interid){
        return interinfoPageService.getIngerinfforid(interid);
    }

    @RequestMapping(value = "/delinterinfo",method = RequestMethod.POST)
    @ResponseBody
    public Object delInterinfo(@RequestParam(value = "interid") Integer interid){
        try{
            interinfoPageService.delinterinfo(interid);
            return new ResPonseinfo(0,"删除成功");
        }
        catch (Exception e){
            logger.error("delinterinfo is system error --->"+e);
            return new ResPonseinfo(-1,"系统错误");
        }
    }
}
